package edu.itla.medifacil.controllers;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.itla.medifacil.helpers.Utilities;
import edu.itla.medifacil.persistence.entities.People;
import edu.itla.medifacil.persistence.entities.Test;
import edu.itla.medifacil.persistence.entities.TestResult;
import edu.itla.medifacil.persistence.enums.Role;
import edu.itla.medifacil.persistence.managers.PersonManager;
import edu.itla.medifacil.persistence.managers.TestManager;
import edu.itla.medifacil.persistence.managers.TestResultManager;

/**
 * Servlet implementation class Test
 */

public class Tests extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final TestManager testManager;
	private final TestResultManager testResultManager;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Tests() {
        super();
        this.testManager = new TestManager();
        this.testResultManager = new TestResultManager();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PersonManager personManager = new PersonManager();

		if( "add".equals( request.getParameter("action") ) ){
			List<People> doctors  = personManager.findAll( Role.DOCTOR.getId() );
			List<People> patients  = personManager.findAll( Role.PATIENT.getId() );
			List<Test> tests = testManager.findAll();
			
			request.getSession(true).setAttribute( "doctors", doctors );
			request.getSession().setAttribute( "patients", patients );
			request.getSession().setAttribute( "tests", tests );
			
			response.sendRedirect("tests/add.jsp");
		}else if( "view".equals( request.getParameter("action") ) ){
			TestResult testResult = testResultManager.find( Integer.parseInt( request.getParameter("id") ) );
			request.getSession().setAttribute("test", testResult);
			response.sendRedirect("tests/view.jsp");
		}else if( "edit".equals( request.getParameter("action") ) ){
			TestResult testResult = testResultManager.find( Integer.parseInt( request.getParameter("id") ) );
			request.getSession().setAttribute("test", testResult);
			List<People> doctors  = personManager.findAll( Role.DOCTOR.getId() );
			List<People> patients  = personManager.findAll( Role.PATIENT.getId() );
			List<Test> tests = testManager.findAll();
			
			request.getSession(true).setAttribute( "doctors", doctors );
			request.getSession().setAttribute( "patients", patients );
			request.getSession().setAttribute( "tests", tests );
			response.sendRedirect("tests/edit.jsp");
		}else{
			List<TestResult> testResult = testResultManager.findAll();
			request.getSession(true).setAttribute( "tests", testResult );
			response.sendRedirect("tests/");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if( "save".equals( request.getParameter("action") ) ){
			Map<String, String> parameters = Utilities.parametersArrayToMap( request.getParameterMap() );
			testResultManager.save(parameters);
			response.sendRedirect("Tests.do");
		}else if( "addTest".equals( request.getParameter("action") ) ){
			Map<String, String> parameters = Utilities.parametersArrayToMap( request.getParameterMap() );
			testManager.save(parameters);
		}
	}

}
